package org.future.dataanalysis;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.future.dataanalysis.bean.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author Dongwei-Chen
 * @Date 2023/12/25 19:53
 * @Description
 */
public class DataLogModelAnalysis extends DataModelAnalysis {

    private final Logger log = LoggerFactory.getLogger(DataLogModelAnalysis.class);

    public DataLogModelAnalysis(Properties properties, String userName, Properties oracleProperties) {
        super(properties);
        initCfg(userName, oracleProperties);
    }

    protected void initCfg(String userName, Properties oracleProperties) {

    }

    @Override
    protected RecursionResult getData(JSON dataObj, String parent, Node node, RecursionResult data, Type type) {
        if (dataObj == null) {
            return data;
        }
        Map<String, String> dataMap = node.getDataMap();
        if (type == null) {
            type = node.getType();
        }
        switch (type) {
            case OBJECT:
                JSONObject jsonObject = (JSONObject) dataObj;
                AnalysisServer.fullData(data, dataMap, jsonObject);

                Map<String, Node> objMap = node.getObjMap();
                if (objMap != null && !objMap.isEmpty()) {

                    for (Map.Entry<String, Node> objEntity : objMap.entrySet()) {
                        String fieldName = objEntity.getKey();
                        Node childNode = objEntity.getValue();
                        Object json = jsonObject.get(fieldName);
                        JSON jsonObj;
                        if (childNode.getType() == Type.OBJECT && json instanceof String) {
                            String jsonString = (String) json;
                            jsonObj = JSON.parseObject(jsonString);
                        } else {
                            jsonObj = (JSON) json;
                        }
                        data = getData(jsonObj, fieldName, childNode, data, null);
                    }
                }

                Map<String, Node> nodeMap = node.getNodeMap();
                if (nodeMap == null || nodeMap.isEmpty()) {
                    return data;
                }
                for (Map.Entry<String, Node> entry : nodeMap.entrySet()) {
                    String fieldName = entry.getKey();
                    Node childNode = entry.getValue();
                    JSON jsonObj = (JSON) jsonObject.get(fieldName);
                    data = getData(jsonObj, fieldName, childNode, data, null);
                }
                break;
            case ARRAY:
                JSONArray jsonArray = (JSONArray) dataObj;
                RecursionResult singleResult = null;
                if (BUSINESS_OBJECT_LIST.equals(parent)) {
                    //此时 初始化
                    singleResult = new RecursionResult();
                    singleResult.setData(data.getData());
                }

                for (Object json : jsonArray) {
                    JSONObject dataItem = (JSONObject) json;
                    if (DATA_ITEM_LIST.equals(parent)) {
                        //此处开始 解析dataItemList数据
                        DataUnit unit = data.getData();
                        Map<String, Object> dataArea = unit.getDataArea();
                        String cmd = (String) dataArea.get("CMD_TYPE");
                        if (StringUtils.isEmpty(cmd)) {
                            continue;
                        }
                        List<AnalysisServer> analysisServerList = null;
                        if (cmd.contains("SET")) {
                            analysisServerList = analysisServerMap.get("SET");
                        } else if (cmd.contains("GET")) {
                            analysisServerList = analysisServerMap.get("GET");
                        } else if (cmd.contains("ACTION")) {
                            analysisServerList = analysisServerMap.get("ACTION");
                        }
                        if (CollectionUtil.isEmpty(analysisServerList)) {
                            continue;
                        }
                        for (AnalysisServer analysisServer : analysisServerList) {
                            DataUnit dataUnit = new DataUnit();
                            BeanUtil.copyProperties(unit, dataUnit);
                            dataUnit.setDataArea(new HashMap<>(unit.getDataArea()));
                            RecursionResult analysis = analysisServer.analysis(dataItem, dataUnit);
                            data.getAllDataUnit().addAll(analysis.getAllDataUnit());
                        }
                        continue;
                    }
                    DataUnit dataUnit;
                    if (singleResult != null) {
                        dataUnit = singleResult.getData();
                    } else {
                        dataUnit = data.getData();
                    }
                    DataUnit unit = new DataUnit();
                    BeanUtil.copyProperties(dataUnit, unit);
                    data.setData(unit);
                    data = getData(dataItem, parent, node, data, Type.OBJECT);
                    assert data != null;
                    if (node.isFlat()) {
                        data.pushFinalArea(data.getData());
                    }
                }
            default:
        }
        return data;
    }
}
